﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LotteryVote.Data;
using LotteryVote.Entity;
using LotteryVote.Logic.Exceptions;

namespace LotteryVote.Logic
{
    public class NumLenFacade
    {
        private NumLengthDataAccess _daNumLen;
        internal NumLengthDataAccess DaNumLen
        {
            get
            {
                if (_daNumLen == null)
                    _daNumLen = new NumLengthDataAccess();
                return _daNumLen;
            }
            set
            {
                _daNumLen = value;
            }
        }

        private CompanyTypeFacade _comTypeFacade;
        internal CompanyTypeFacade CompanyTypeFacade
        {
            get
            {
                if (_comTypeFacade == null)
                    _comTypeFacade = new CompanyTypeFacade();
                return _comTypeFacade;
            }
            set
            {
                _comTypeFacade = value;
            }
        }

        private CompanyFacade _comFacade;
        internal CompanyFacade CompanyFacade
        {
            get
            {
                if (_comFacade == null)
                    _comFacade = new CompanyFacade();
                return _comFacade;
            }
            set
            {
                _comFacade = value;
            }
        }

        /// <summary>
        /// 添加号码长度记录.
        /// </summary>
        /// <param name="length">The length.</param>
        /// <returns></returns>
        public NumLength AddNumLength(int length)
        {
            return this.AddNumLength(length, length + "D", string.Empty);
        }
        /// <summary>
        /// 添加号码长度记录.
        /// </summary>
        /// <param name="length">号码长度.</param>
        /// <param name="description">描述.</param>
        /// <returns></returns>
        public NumLength AddNumLength(int length, string description)
        {
            return this.AddNumLength(length, length + "D", description);
        }
        /// <summary>
        /// 添加号码长度记录.
        /// </summary>
        /// <param name="length">号码长度.</param>
        /// <param name="lenName">名称.</param>
        /// <param name="descriptin">描述.</param>
        /// <returns></returns>
        public NumLength AddNumLength(int length, string lenName, string descriptin)
        {
            if (string.IsNullOrEmpty(lenName))
                throw new ArgumentNullException("lenName");
            NumLength numLen = new NumLength
            {
                Length = length,
                LenName = lenName,
                Description = descriptin
            };
            DaNumLen.InsertNumLength(numLen);
            return numLen;
        }
        /// <summary>
        /// 根据号码长度的Id删除记录.
        /// </summary>
        /// <param name="lenId">The len id.</param>
        public void RemoveNumLengthByLenId(int lenId)
        {
            DaNumLen.DeleteNumLength(lenId);
        }
        /// <summary>
        /// 根据长度删除记录.
        /// </summary>
        /// <param name="len">The len.</param>
        public void RemoveNumLengthByLength(int len)
        {
            DaNumLen.DeleteNumLengthByLen(len);
        }

        /// <summary>
        /// 添加公司类型支持号码长度.
        /// </summary>
        /// <param name="companyTypeId">公司类型Id.</param>
        /// <param name="numLenId">号码长度Id.</param>
        /// <param name="count">支持该长度号码的数量.</param>
        public void AddCompanyTypeSupportNumLen(int companyTypeId, int numLenId, int count)
        {
            var companyType = CompanyTypeFacade.GetCompanyType(companyTypeId);
            if (companyType == null)
                throw new InvalidDataException("companyType,companyTypeId:" + companyTypeId);
            var numLen = DaNumLen.GetNumLength(numLenId);
            if (numLen == null)
                throw new InvalidDataException("NumLength,lenId:" + numLenId);
            DaNumLen.InsertCompanyTypeSupportNumLen(companyType, numLen, count);
        }
        /// <summary>
        /// 添加公司类型支持号码长度
        /// </summary>
        /// <param name="companyTypeId">公司类型Id..</param>
        /// <param name="len">号码的长度.</param>
        /// <param name="count">支持该长度号码的数量.</param>
        public void AddCompanyTypeSupportNumLength(int companyTypeId, int len, int count)
        {
            var companyType = CompanyTypeFacade.GetCompanyType(companyTypeId);
            if (companyType == null)
                throw new InvalidDataException("companyType,companyTypeId:" + companyTypeId);
            var numLen = DaNumLen.GetNumLegthByLength(len);
            if (numLen == null)
                numLen = this.AddNumLength(len);
            DaNumLen.InsertCompanyTypeSupportNumLen(companyType, numLen, count);
        }
        /// <summary>
        /// 更新公司类型支持号码长度.
        /// </summary>
        /// <param name="companyTypeId">公司类型Id.</param>
        /// <param name="numLenId">号码长度Id.</param>
        /// <param name="count">支持该长度号码的数量.</param>
        public void UpdateCompanyTypeSupportNumLen(int companyTypeId, int numLenId, int count)
        {
            var companyType = CompanyTypeFacade.GetCompanyType(companyTypeId);
            if (companyType == null)
                throw new InvalidDataException("companyType,companyTypeId:" + companyTypeId);
            var numLen = DaNumLen.GetNumLength(numLenId);
            if (numLen == null)
                throw new InvalidDataException("NumLength,lenId:" + numLenId);
            DaNumLen.UpdateCompanyTypeSupportNumLen(companyType, numLen, count);
        }
        /// <summary>
        /// 更新公司类型支持号码长度.
        /// </summary>
        /// <param name="companyTypeId">公司类型Id.</param>
        /// <param name="len">号码的长度.</param>
        /// <param name="count">支持该长度号码的数量.</param>
        public void UpdateCompanyTypeSupportNumLength(int companyTypeId, int len, int count)
        {
            var companyType = CompanyTypeFacade.GetCompanyType(companyTypeId);
            if (companyType == null)
                throw new InvalidDataException("companyType,companyTypeId:" + companyTypeId);
            var numLen = DaNumLen.GetNumLegthByLength(len);
            if (numLen == null)
                numLen = this.AddNumLength(len);
            DaNumLen.UpdateCompanyTypeSupportNumLen(companyType, numLen, count);
        }
        /// <summary>
        /// 移除公司类型支持号码长度.
        /// </summary>
        /// <param name="companyTypeId">公司类型Id.</param>
        /// <param name="numLenId">号码长度Id.</param>
        public void RemoveCompanyTypeSupportNumLen(int companyTypeId, int numLenId)
        {
            DaNumLen.DeleteCompanyTypeSupportNumLen(
                new CompanyTypeModel { Id = companyTypeId },
                new NumLength { LenId = numLenId });
        }

        /// <summary>
        /// 是否已存在指定长度的号码长度记录.
        /// </summary>
        /// <param name="len">长度.</param>
        /// <returns>
        ///   <c>true</c> if [is exist num length] [the specified len]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsExistNumLength(int len)
        {
            return DaNumLen.GetNumLegthByLength(len) != null;
        }
        /// <summary>
        /// 公司类型是否支持指定号码长度
        /// </summary>
        /// <param name="companyId">公司类型Id.</param>
        /// <param name="numLenId">号码长度Id.</param>
        /// <returns>
        ///   <c>true</c> if [is company support num len] [the specified company id]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsCompanyTypeSupportNumLen(int companyId, int numLenId)
        {
            var companyType = CompanyTypeFacade.GetCompanyTypeByCompanyId(companyId);
            if (companyType == null)
                throw new InvalidDataException("companyType,companyId:" + companyId);
            return DaNumLen.GetSupportNumLength(companyType.Id, numLenId) != null;
        }

        /// <summary>
        /// 获取指定公司支持的号码长度.
        /// </summary>
        /// <param name="companyId">The company id.</param>
        /// <returns></returns>
        public IList<CompanyTypeSupportNumLen> GetNumLengthByCompanyId(int companyId)
        {
            var companyType = CompanyTypeFacade.GetCompanyTypeByCompanyId(companyId);
            if (companyType == null)
                throw new InvalidDataException("companyType,companyId:" + companyId);
            return DaNumLen.GetSupportNumLengthByType(companyType);
        }
        /// <summary>
        /// 根据公司类型获取所支持的号码长度.
        /// </summary>
        /// <param name="companyTypeId">The company type id.</param>
        /// <returns></returns>
        public IList<CompanyTypeSupportNumLen> GetNumLengthByCompanyType(int companyTypeId)
        {
            //TODO:引入空对象实例能更好的处理
            var companyType = CompanyTypeFacade.GetCompanyType(companyTypeId);
            if (companyType == null)
                throw new InvalidDataException("companyType,companyTypeId:" + companyTypeId);
            return DaNumLen.GetSupportNumLengthByType(companyType);
        }
        /// <summary>
        /// 获取所有号码长度记录.
        /// </summary>
        /// <returns></returns>
        public IList<NumLength> GetAllNumLength()
        {
            return DaNumLen.GetAllNumLength();
        }
    }
}
